package com.group3.merchant.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.group3.entity.Merchant;
import com.group3.merchant.service.IMerchantService;
import com.group3.merchant.service.impl.MerchantServiceImpl;

/**
 * Servlet implementation class UpdateMerchServlet
 */
public class UpdateMerchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IMerchantService service = new MerchantServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateMerchServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean isSuccess = motify(request, response);
		if (isSuccess) {
			response.sendRedirect("reject.jsp");
		} else {
			System.out.println("resubmit fail");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean isSuccess = motify(request, response);
		if (isSuccess) {
			response.sendRedirect("reject.jsp");
		} else {
			System.out.println("resubmit fail");
		}
	}

	private boolean motify(HttpServletRequest request,
			HttpServletResponse response) {
		boolean isSuccess = false;
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (isMultipart) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			String filePath = "";
			Merchant merchant = (Merchant) request.getSession().getAttribute("loginToken");
			System.out.println("session Merchant: " + merchant);
			
			
			try {
				List<FileItem> list = upload.parseRequest(request);
				Iterator<FileItem> i = list.iterator();
				while (i.hasNext()) {
					FileItem item = i.next();
					if (item.isFormField()) {
						switch (item.getFieldName()) {
						case "mName":
							if(!"".equals(item.getString("utf-8")))
							{merchant.setmName(item.getString("utf-8"));}
							break;
						case "address":
							if(!"".equals(item.getString("utf-8")))
							{merchant.setAddress(item.getString("utf-8"));}
							break;
						default:
							break;
						}
					} else {
						switch (item.getFieldName()) {
						case "image":
							if(!"".equals(item.getString("utf-8"))){
							File imageFile = new File("d://AMC/Header/"
									+ UUID.randomUUID() + item.getName());
							filePath = imageFile.getAbsolutePath();
							merchant.setImageUrl(filePath);
							item.write(imageFile);}
							System.out.println(filePath);
							break;
						case "idCard":
							if(!"".equals(item.getString("utf-8"))){
//								System.out.println("if");
							File idCardFile = new File("d://AMC/IDCard/"
									+ UUID.randomUUID() + item.getName());
							filePath = idCardFile.getAbsolutePath();
							merchant.setIdCardUrl(filePath);
							item.write(idCardFile);
							}
							else {
//								System.out.println("else");
							}
							break;
						default:
							break;
						}
					}
				}
				// 调service
				System.out.println("Merchant: ..."+ merchant);
				Merchant merchant2 = service.motify(merchant);
				System.out.println("Update Merchant: " + merchant2);
				HttpSession session = request.getSession();
				session.setAttribute("loginToken", merchant2);
				if (merchant2 != null)
					isSuccess = true;
				
				System.out.println("---" + merchant);
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return isSuccess;
	}

}
